//
//  AddinfViewController.m
//  MidProject
//
//  Created by itlab on 2020/11/15.
//  Copyright © 2020 itlab. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AddinfViewController.h"
#import "Masonry.h"
#import "DLPhotoPicker.h"
#import <AFNetworking/AFNetworking.h>


@interface AddinfViewController ()

@property  PersonViewController * list;
@property  TableListController * owner_table_list;
@property(nonatomic, strong) NSDictionary *PostItem;
@property(nonatomic, strong) NSDictionary *PostRes;
@property(nonatomic, strong) NSDictionary *GetItem;
@property(nonatomic, strong) NSDictionary *DeleteItem;
@end


@implementation AddinfViewController

- (instancetype) initWithNSDictionary: (NSDictionary *) Data{
    self = [super init];
    if (self != nil){
        self.title_text = [[UITextField alloc] init];
        self.title_text.text = [Data valueForKey:@"Name"];
        self.words_text = [[UITextView alloc] init];
        self.words_text.text = [Data valueForKey:@"Detail"];
        self.assets = [[NSMutableArray alloc] init];
        NSArray *temp_img = [[Data valueForKeyPath:@"Album"] valueForKey:@"Images"];
        if (temp_img.count > 0){
            NSString *before_url = @"http://172.18.178.56/api/thumb/";
            for (int i = 0; i < temp_img.count; ++i){
                NSString *img_url = [temp_img[i] valueForKeyPath:@"Thumb"];
                NSString *all_url = [before_url stringByAppendingString:img_url];
                NSURL *imageURL = [NSURL URLWithString:all_url];
                NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
                UIImage *temp_uiimage = [UIImage imageWithData:imageData];
                [self.assets addObject:temp_uiimage];
            }
        }
        NSLog(@"my self.asset.count %lu",(unsigned long)self.assets.count);
    }
    self.editData = Data;
    return  self;
}

- (void)setPersonViewController:(PersonViewController *)list{
    self.list = list;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    if (self.assets == nil){
        self.assets = [[NSMutableArray alloc] init];
    }
    self.images = [[NSMutableArray alloc] init];
    self.delete_signal = [[NSMutableArray alloc] init];
    self.send_To_Public = false;
    self.fileData = [[NSData alloc] init];
    
    UILabel * time = [[UILabel alloc] init];
    UILabel * isPublic = [[UILabel alloc] init];
    UILabel * words = [[UILabel alloc] init];
    self.picture = [[UILabel alloc] init];
    
    //[time setBackgroundColor:[UIColor blueColor]];
    time.text = @"标题";
    [time setFont:[UIFont systemFontOfSize:15]];
    isPublic.text = @"公开";
    [isPublic setFont:[UIFont systemFontOfSize:15]];
    words.text = @"详情";
    [words setFont:[UIFont systemFontOfSize:15]];
    self.picture.text = @"配图";
    [self.picture setFont:[UIFont systemFontOfSize:15]];
    
    [self.view addSubview:time];
    [self.view addSubview:isPublic];
    [self.view addSubview:words];
    [self.view addSubview:self.picture];
    
    [time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60);
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.view).offset(70);
        make.height.mas_equalTo(20);
    }];
    [isPublic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(time.mas_width);
        make.left.equalTo(self.view).offset(20);
        make.bottom.equalTo(self.view).offset(-70);
        make.height.mas_equalTo(20);
    }];
    [words mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(time.mas_width);
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(time.mas_bottom).offset(20);
        make.height.mas_equalTo(20);
    }];
    
    if (self.title_text == nil){
        self.title_text = [[UITextField alloc] init];
    }
    if (self.words_text == nil){
        self.words_text = [[UITextView alloc] init];
    }
    self.isPublic_choose = [[UISwitch alloc] init];
    [_isPublic_choose addTarget:self action:@selector(choosePublic) forControlEvents:UIControlEventValueChanged];
    
    [self.title_text setPlaceholder:@"单行输入"]; // 占位文本
    [self.title_text setSecureTextEntry:NO];
    [self.title_text setKeyboardType:UIKeyboardTypeNumberPad];
    [self.title_text setReturnKeyType:UIReturnKeyDone];
    [self.title_text setBackgroundColor:[UIColor whiteColor]];
    [self.title_text setTextColor:[UIColor blackColor]];
    [self.title_text setBorderStyle:UITextBorderStyleRoundedRect];
    [self.title_text setFont:[UIFont systemFontOfSize:12]];
    
    
    // 设置边框颜色为灰色；
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){0.1,0,0,0.1});
    
    // 设置图片的边框的宽度，颜色和形状；
    [self.words_text.layer setBorderWidth:2];
    [self.words_text.layer setBorderColor:color];
    [self.words_text.layer setCornerRadius:5];
    [self.words_text setSecureTextEntry:NO];
    [self.words_text setKeyboardType:UIKeyboardTypeNumberPad];
    [self.words_text setReturnKeyType:UIReturnKeyDone];
    [self.words_text setBackgroundColor:[UIColor whiteColor]];
    [self.words_text setTextColor:[UIColor blackColor]];
    [self.words_text setFont:[UIFont systemFontOfSize:12]];
    
    [self.view addSubview:self.title_text];
    [self.view addSubview:self.isPublic_choose];
    [self.view addSubview:self.words_text];
    
    [self.title_text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view.frame.size.width-100);
        make.left.equalTo(time.mas_left).offset(60);
        make.top.equalTo(time.mas_top);
        make.height.mas_equalTo(20);
    }];
    [self.words_text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view.frame.size.width-100);
        make.left.equalTo(words.mas_left).offset(60);
        make.top.equalTo(words.mas_top);
        make.height.mas_equalTo(300);
    }];
    [self.picture mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(time.mas_width);
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.words_text.mas_bottom).offset(20);
        make.height.mas_equalTo(20);
    }];
    [self.isPublic_choose mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(isPublic.mas_left).offset(60);
        make.top.equalTo(isPublic.mas_top);
    }];
    
    
    int x, y;
    y = 0;
    x = 60;
    for (int i = 0; i < self.assets.count; ++i){
        UIImage * image = self.assets[i];
        UIView * img_button_view = [[UIView alloc] init];
        UIButton * imagebutton = [[UIButton alloc] init];
        UIButton * delete = [[UIButton alloc] initWithFrame:CGRectMake(60, 0, 16, 16)];
        [delete setImage:[UIImage imageNamed:@"chahao.png"] forState:UIControlStateNormal];
        delete.alpha = 0;
        [delete addTarget:self action:@selector(deleteimage:) forControlEvents:UIControlEventTouchDown];
        [imagebutton setBackgroundImage:image forState:UIControlStateNormal];
        [imagebutton addTarget:self action:@selector(imageclick:) forControlEvents:UIControlEventTouchDown];
        [imagebutton addTarget:self action:@selector(doubletouch:) forControlEvents:UIControlEventTouchDownRepeat];
        [img_button_view addSubview:imagebutton];
        [img_button_view addSubview:delete];
        [self.images addObject:img_button_view];
        [self.delete_signal addObject: delete];
        [self.view addSubview:img_button_view];
        
        imagebutton.tag = 120+i;
        delete.tag = 240 + i;
        [imagebutton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(img_button_view).offset(10);
            make.top.equalTo(img_button_view).offset(10);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(60);
        }];
        [img_button_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.picture.mas_left).offset(x-10 + (i%3) * 80);
            make.top.equalTo(self.picture.mas_top).offset(y-10 + (i/3) * 80);
            make.width.mas_equalTo(75);
            make.height.mas_equalTo(75);
        }];
    }
    
    if ([self.assets count] < 6){
        NSLog(@"here is view did load %lu",(unsigned long)self.assets.count);
        self.button = [[UIButton alloc] init];
        self.button.tag = 100;
        [self.button setImage:[UIImage imageNamed:@"jiahao.png"] forState:UIControlStateNormal];
        [self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.button];
        [self.button  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(60);
            make.left.equalTo(self.picture.mas_left).offset(x + (self.assets.count%3) * 80);
            make.top.equalTo(self.picture.mas_top).offset(y + (self.assets.count/3) * 80);
            make.height.mas_equalTo(60);
        }];
    }
    
    
    UIButton * fabu = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [fabu setTitle:@"发布" forState:UIControlStateNormal];
    [fabu.layer setBorderWidth:2];
    [fabu.layer setBorderColor:color];
    [fabu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [fabu setBackgroundColor:[UIColor whiteColor]];
    fabu.titleLabel.font = [UIFont systemFontOfSize: 12.0];
    fabu.layer.cornerRadius = 10;
    [fabu addTarget:self action:@selector(fabu_on_click:) forControlEvents:UIControlEventTouchDown];
    
    [self.view addSubview:fabu];
    [fabu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60);
        make.right.equalTo(self.view).offset(-20);
        make.bottom.equalTo(self.view).offset(-60);
        make.height.mas_equalTo(30);
    }];
    
}

- (void)fabu_on_click: (id)sender{
    if (self.send_To_Public){
        self.PostItem = @{
                          @"title":self.title_text.text,
                          @"detail": self.words_text.text,
                          @"isPublic": @YES
                          };
    }
    else{
        self.PostItem = @{
                          @"title":self.title_text.text,
                          @"detail": self.words_text.text,
                          @"isPublic": @NO
                          };
    }
    
    // 初始化对话框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认发布？" preferredStyle:UIAlertControllerStyleAlert];
    // 确定发布
    UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        if (self.editData != nil){
            NSLog(@"%@", [NSString stringWithFormat:@"http://172.18.178.56/api/content/%@",[self.editData valueForKey:@"ID"]]);
            [self DeleteSelf:[NSString stringWithFormat:@"http://172.18.178.56/api/content/%@",[self.editData valueForKey:@"ID"]]];
        }
        if (self.assets.count > 0){
            [self addPicture];
        }
        else {
            [self PostSelf:@"http://172.18.178.56/api/content/text"];
        }
//        if (self.owner_table_list != nil){
//            [self.owner_table_list reloadData];
//        }else{
//            [self.list reloadData];
//        }
        [self clear];
        // 设置转场动画；
        //1.初始化
        CATransition  *transition = [CATransition animation];
        //2.设置动画时长,设置代理人
        transition.duration = 1.0f;
        transition.delegate = self;
        //3.设置切换速度效果
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        /*枚举值:
         kCAMediaTimingFunctionLinear
         kCAMediaTimingFunctionEaseIn
         kCAMediaTimingFunctionEaseOut
         kCAMediaTimingFunctionEaseInEaseOut
         kCAMediaTimingFunctionDefault*/
        //4.动画切换风格
        transition.type = kCATransitionPush;
        /*枚举值:
         kCATransitionFade = 1,     // 淡入淡出
         kCATransitionPush,         // 推进效果
         kCATransitionReveal,       // 揭开效果
         kCATransitionMoveIn,       // 慢慢进入并覆盖效果*/
        //5.动画切换方向
        transition.subtype = kCATransitionFromLeft;//左侧
        /*枚举值:
         kCATransitionFromRight//右侧
         kCATransitionFromLeft//左侧
         kCATransitionFromTop//顶部
         kCATransitionFromBottom//底部*/
        //6.进行跳转
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
        //        [self.navigationController pushViewController:"你要跳转的页面" animated:NO];
        //跳转动画一定设置为NO,不然会两个效果叠加
        [self.navigationController popViewControllerAnimated:YES];
        //        [self performSegueWithIdentifier:@"Logout" sender:nil];
    }];
    UIAlertAction * cancelAction =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    
    // 弹出对话框
    [self presentViewController:alert animated:true completion:nil];
}

- (void)clear{
    self.title_text.text = @"";
    self.words_text.text = @"";
    for(int i = 0; i < self.assets.count; ++i){
        [[self.view viewWithTag:120+i] removeFromSuperview];
    }
    [[self.view viewWithTag:100] removeFromSuperview];
    self.button = [[UIButton alloc] init];
    self.button.tag = 100;
    [self.button setImage:[UIImage imageNamed:@"jiahao.png"] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    [self.button  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60);
        make.left.equalTo(self.picture.mas_left).offset(60);
        make.top.equalTo(self.picture.mas_top).offset(0);
        make.height.mas_equalTo(60);
    }];
    [self.assets removeAllObjects];
}

-(void)buttonClicked:(id)sender{
    DLPhotoPickerViewController *picker = [[DLPhotoPickerViewController alloc] init];
    picker.delegate = self;
    picker.pickerType = DLPhotoPickerTypePicker;
    picker.navigationTitle = NSLocalizedString(@"Albums", nil);
    
    [self presentViewController:picker animated:YES completion:nil];
}


#pragma mark DLPhotoPickerViewControllerDelegate

// cancel
- (void)pickerControllerDidCancel:(DLPhotoPickerViewController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 从相册拿出图片；
// assets为拿出的图片；
-(void)pickerController:(DLPhotoPickerViewController *)picker didFinishPickingAssets:(NSArray *)pickassets
{
    [self dismissViewControllerAnimated:YES completion:nil];
    int begin = self.assets.count;
    //[self.assets addObjectsFromArray:[NSMutableArray arrayWithArray:assets]];
    
    int x, y;
    y = 0;
    x = 60;
    for (int i = begin; i < begin + pickassets.count; ++i){
        DLPhotoAsset *asset = [pickassets objectAtIndex:i-begin];
        CGSize targetSize = CGSizeMake(100, 100);
        [asset requestThumbnailImageWithSize:targetSize completion:^(UIImage *image, NSDictionary *info) {
            [self.assets addObject:image];
            UIView * img_button_view = [[UIView alloc] init];
            UIButton * imagebutton = [[UIButton alloc] init];
            UIButton * delete = [[UIButton alloc] initWithFrame:CGRectMake(60, 0, 16, 16)];
            [delete setImage:[UIImage imageNamed:@"chahao.png"] forState:UIControlStateNormal];
            delete.alpha = 0;
            [delete addTarget:self action:@selector(deleteimage:) forControlEvents:UIControlEventTouchDown];
            [imagebutton setBackgroundImage:image forState:UIControlStateNormal];
            [imagebutton addTarget:self action:@selector(imageclick:) forControlEvents:UIControlEventTouchDown];
            [imagebutton addTarget:self action:@selector(doubletouch:) forControlEvents:UIControlEventTouchDownRepeat];
            [img_button_view addSubview:imagebutton];
            [img_button_view addSubview:delete];
            [self.images addObject:img_button_view];
            [self.delete_signal addObject: delete];
            [self.view addSubview:img_button_view];
            
            imagebutton.tag = 120+i;
            delete.tag = 240 + i;
            [imagebutton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(img_button_view).offset(10);
                make.top.equalTo(img_button_view).offset(10);
                make.width.mas_equalTo(60);
                make.height.mas_equalTo(60);
            }];
            [img_button_view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.picture.mas_left).offset(x-10 + (i%3) * 80);
                make.top.equalTo(self.picture.mas_top).offset(y-10 + (i/3) * 80);
                make.width.mas_equalTo(75);
                make.height.mas_equalTo(75);
            }];
        }];
    }
    
    [[self.view viewWithTag:100] removeFromSuperview];
    if ([self.assets count] < 6){
        self.button = [[UIButton alloc] init];
        self.button.tag = 100;
        [self.button setImage:[UIImage imageNamed:@"jiahao.png"] forState:UIControlStateNormal];
        [self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.button];
        [self.button  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(60);
            make.left.equalTo(self.picture.mas_left).offset(x + (self.assets.count%3) * 80);
            make.top.equalTo(self.picture.mas_top).offset(y + (self.assets.count/3) * 80);
            make.height.mas_equalTo(60);
        }];
    }
}

-(void) imageclick:(UIButton *)sender{
    NSLog(@"ready delete");
    sender.alpha = 0.3;
    self.delete_signal[sender.tag - 120].alpha = 1;
}

-(void) doubletouch:(UIButton *)sender{
    NSLog(@"cancel delete");
    sender.alpha = 1;
    self.delete_signal[sender.tag - 120].alpha = 0;
}

-(void) deleteimage: (UIButton *)sender{
    NSLog(@"delete");
    if (sender.alpha == 1){
        [self.assets removeObjectAtIndex:sender.tag - 240];
        [self reloadImage];
    }
}

-(void) choosePublic{
    self.send_To_Public = self.send_To_Public == false;
}

-(void) reloadImage{
    for (int i = 0; i < self.images.count; ++i){
        [self.images[i] removeFromSuperview];
        [self.delete_signal removeAllObjects];
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
    int x, y;
    y = 0;
    x = 60;
    for (int i = 0; i < self.assets.count; ++i){
        UIImage * image = self.assets[i];
        UIView * img_button_view = [[UIView alloc] init];
        UIButton * imagebutton = [[UIButton alloc] init];
        UIButton * delete = [[UIButton alloc] initWithFrame:CGRectMake(60, 0, 16, 16)];
        [delete setImage:[UIImage imageNamed:@"chahao.png"] forState:UIControlStateNormal];
        delete.alpha = 0;
        [delete addTarget:self action:@selector(deleteimage:) forControlEvents:UIControlEventTouchDown];
        [imagebutton setBackgroundImage:image forState:UIControlStateNormal];
        [imagebutton addTarget:self action:@selector(imageclick:) forControlEvents:UIControlEventTouchDown];
        [imagebutton addTarget:self action:@selector(doubletouch:) forControlEvents:UIControlEventTouchDownRepeat];
        [img_button_view addSubview:imagebutton];
        [img_button_view addSubview:delete];
        [self.images addObject:img_button_view];
        [self.delete_signal addObject: delete];
        [self.view addSubview:img_button_view];
        
        imagebutton.tag = 120+i;
        delete.tag = 240 + i;
        [imagebutton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(img_button_view).offset(10);
            make.top.equalTo(img_button_view).offset(10);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(60);
        }];
        [img_button_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.picture.mas_left).offset(x-10 + (i%3) * 80);
            make.top.equalTo(self.picture.mas_top).offset(y-10 + (i/3) * 80);
            make.width.mas_equalTo(75);
            make.height.mas_equalTo(75);
        }];
    }
    
    [[self.view viewWithTag:100] removeFromSuperview];
    if ([self.assets count] < 6){
        self.button = [[UIButton alloc] init];
        self.button.tag = 100;
        [self.button setImage:[UIImage imageNamed:@"jiahao.png"] forState:UIControlStateNormal];
        [self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.button];
        [self.button  mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(60);
            make.left.equalTo(self.picture.mas_left).offset(x + (self.assets.count%3) * 80);
            make.top.equalTo(self.picture.mas_top).offset(y + (self.assets.count/3) * 80);
            make.height.mas_equalTo(60);
        }];
    }
}

//在当前页面增添
//@property(nonatomic, strong) NSDictionary *PostItem;
//@property(nonatomic, strong) NSDictionary *PostRes;
//每次调用的时候需要先更新页面的PostItem，针对不同的Post操作，有不同的Dictionary和urlString
-(void) PostSelf:(NSString *)urlString{
    NSDictionary *item = [[NSDictionary alloc]initWithDictionary:self->_PostItem];
    NSLog(@"%@",item);
    NSURL *url = [NSURL URLWithString:urlString];
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:item options:NSJSONWritingPrettyPrinted error:&error];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:jsonData];
    NSURLSession *session = [NSURLSession sharedSession];
    dispatch_semaphore_t semaphore= dispatch_semaphore_create(0);
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                                completionHandler:^(NSData *return_data, NSURLResponse *response, NSError *error) {
                                                    NSError *jsonError;
                                                    self->_PostRes = [NSJSONSerialization JSONObjectWithData:return_data options:kNilOptions error:&jsonError];
                                                    NSLog(@"%@",self->_PostRes);
                                                    if(jsonError) {
                                                        // check the error description
                                                        NSLog(@"json error : %@", [jsonError localizedDescription]);
                                                    } else {
                                                        // use the jsonDictionaryOrArray
                                                        if ([self->_PostRes[@"State"]  isEqual: @"success"]) {
                                                            //NSLog(@"json : success");
                                                        } else {
                                                            NSLog(@"json : error");
                                                        }
                                                    }
                                                    dispatch_semaphore_signal(semaphore);
                                                }];
    [dataTask resume];
    dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
}

- (void) setTableList:(TableListController *)list{
    self.owner_table_list = list;
}

-(void)addPicture{
    NSString * url = @"http://172.18.178.56/api/content/album";
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    //NSMutableDictionary *params = [NSMutableDictionary dictionary];
    BOOL check_is_public = self.send_To_Public;
    [manager POST:url parameters:self.PostItem headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        for (int i = 0; i < self.assets.count; ++i){
            NSLog(@"send image");
            UIImage * image = self.assets[i];
            self.fileData = UIImageJPEGRepresentation(image, 0.4);
            [formData appendPartWithFileData:self.fileData name:[NSString stringWithFormat:@"file%d",i+1] fileName:[NSString stringWithFormat:@"file%d",i+1] mimeType:@"image/jpg"];
            [formData appendPartWithFileData:self.fileData name:[NSString stringWithFormat:@"thumb%d",i+1] fileName:[NSString stringWithFormat:@"thumb%d",i+1] mimeType:@"image/jpg"];
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

//Delete 操作的API
//当前页面增加的属性
//@property(nonatomic, strong) NSDictionary *DeleteItem;
//Delete 操作的使用样例
// 调用Delete请求的方法
// NSString *urlString = @"http://172.18.178.56/api/content/5fcb5c4ef5beb2def128f01c"; 最后的为要删除的contentID / NotificationID
// [self DeleteSelf:urlString];
-(void)DeleteSelf:(NSString *)urlString{
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"DELETE";
    request.timeoutInterval = 60;
    NSURLSession *session = [NSURLSession sharedSession];
    dispatch_semaphore_t semaphore= dispatch_semaphore_create(0);
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSError *jsonError; self->_DeleteItem = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
        if(jsonError) { // check the error description
            NSLog(@"json error : %@", [jsonError localizedDescription]);
        } else { // use the jsonDictionaryOrArray
            NSLog(@"json : %@",self->_DeleteItem);
        }
        dispatch_semaphore_signal(semaphore);
    }];
        [dataTask resume];
        dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
}
@end
